package com.guava;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;

import java.util.Collections;
import java.util.List;

/**
 * OrderingBaseTester--GuavaTest
 * 排序基础
 *
 * @author: Jovi
 * @createTime: 2018-05-07 11:15
 **/
public class OrderingBaseTester {

    public static void main(String[] args) {

        sortNumbers();

        // sortString();

        // sort();

    }

    private static void sort() {

        Ordering<String> natural = Ordering.natural();

        List<String> list = Lists.newArrayList("peida", "jerry", "harry", "eva", "jhon", "neron");

        System.out.println(list);

        /* 使用toString()返回的字符串按字典顺序进行排序，list必须全部不为null，否则报空指针异常*/
        System.out.println("字典排序后的序列："+Ordering.usingToString().sortedCopy(list));//sortedCopy(Iterable)：返回指定的元素作为一个列表的排序副本

        System.out.println("自然排序后的序列："+ natural.sortedCopy(list));
        System.out.println("自然排序后的序列中最小的K个值："+natural.leastOf(list, 3));
        System.out.println("自然排序后的序列中最大的K个值："+natural.greatestOf(list, 3));
        System.out.println("自然排序后反排序的序列："+ natural.reverse().sortedCopy(list));
        System.out.println("自然排序后的序列是否是有序序列:"+ natural.isOrdered(list));
        System.out.println("自然排序后反排序的序列是否是有序序列:"+ natural.reverse().isOrdered(list));

        System.out.println("===========================================");

        List<Integer> listReduce = Lists.newArrayList();
        for (int i = 9; i > 0; i--) {
            listReduce.add(i);
        }

        List<Integer> listtest = Lists.newArrayList();
        listtest.add(1);
        listtest.add(1);
        listtest.add(1);
        listtest.add(2);

        Ordering<Integer> naturalIntOrder = Ordering.natural();//自然排序

        System.out.println("{1 1 1 2}listTest:" + listtest);
        System.out.println("{1 1 1 2}listTest 是否是有序排序:" + naturalIntOrder.isOrdered(listtest));//是否有序，Iterable不能少于2个元素。
        System.out.println("{1 1 1 2}listTest 是否是严格的有序排序:" + naturalIntOrder.isStrictlyOrdered(listtest));//是否严格有序。请注意，Iterable不能少于两个元素。

        List<Integer> listReduceAfterOrder = naturalIntOrder.sortedCopy(listReduce);
        System.out.println("递减序列listReduce:" + listReduce);
        System.out.println("自然排序后的递减序列listReduceAfterOrder:" + listReduceAfterOrder);

        System.out.println("自然排序后的递减序列listReduceAfterOrder 是否是有序序列:" + naturalIntOrder.isOrdered(listReduceAfterOrder));
        System.out.println("自然排序后的递减序列listReduceAfterOrder 是否是严格的有序序列:" + naturalIntOrder.isStrictlyOrdered(listReduceAfterOrder));

        System.out.println("===========================================");


        List<String> abc = ImmutableList.of("a", "b", "c");
        System.out.println("abc Lists 是否为自然排序：" + natural.isOrdered(abc));
        System.out.println("abc Lists 是否为合格的自然排序：" + natural.isStrictlyOrdered(abc));

        System.out.println("abc Lists 反序 是否为合格的自然排序：" + natural.reverse().isOrdered(abc));

        System.out.println("===========================================");

        List<String> cba = ImmutableList.of("c", "b", "a");
        System.out.println("cba Lists 是否为自然排序："+natural.isOrdered(cba));
        System.out.println("cba Lists 是否为严格的自然排序："+natural.isStrictlyOrdered(cba));
        System.out.println("cba Lists 自然排序后 ："+ (cba = natural.sortedCopy(cba)));

        System.out.println("cba Lists 自然排序后 max:" + natural.max(cba));
        System.out.println("cba Lists 自然排序后 min:" + natural.min(cba));

        System.out.println("cba Lists 自然排序后 最小的K个值:"+natural.leastOf(cba, 2));


    }

    private static void sortString() {
        List<String> names = Lists.newArrayList("Ram", "Shyam", "Mohan", "Sohan", "Ramesh", "Suresh", "Naresh", "Mahesh", null, "Vikas", "Deepak");
        System.out.println("Names List:");
        System.out.println(names);

        Collections.sort(names, Ordering.<String>natural().nullsFirst().reverse());
        System.out.println("Null first then reverse sort list:");
        System.out.println(names);

    }

    private static void sortNumbers() {
        List numbers = Lists.newArrayList(5, 2, 15, 51, 53, 35, 45, 32, 43, 16);

        System.out.println("Input List:");
        System.out.println(numbers);

        Ordering ordering2 = Ordering.arbitrary();//任意排序
        Collections.sort(numbers, ordering2);
        System.out.println("arbitrary List:");
        System.out.println(numbers);

        Ordering ordering = Ordering.natural();//使用Comparable类型的自然顺序， 例如：整数从小到大，字符串是按字典顺序;
        Collections.sort(numbers, ordering);
        System.out.println("Sorted List:");
        System.out.println(numbers);


        System.out.println("=================================");
        System.out.println("List is sorted: " + ordering.isOrdered(numbers));
        System.out.println("Minmum：" + ordering.min(numbers));
        System.out.println("Maxmum：" + ordering.max(numbers));

        Collections.sort(numbers, ordering.reverse());//返回与当前Ordering相反的排序
        System.out.println("Reverse:" + numbers);

        numbers.add(null);
        numbers.add(34);
        numbers.add(null);
        System.out.println("Null added to Sorted List:");
        System.out.println(numbers);

        Collections.sort(numbers, ordering.<Integer>nullsFirst());
        System.out.println("Null first Sorted List :");
        System.out.println(numbers);
        System.out.println("==================================");
    }
}
